﻿using System.Windows;
using System.Windows.Controls;
using LiveTiss.Schemas;
using System.Collections.Generic;
using System.Windows.Data;

namespace LiveTiss.Guias.UserControls
{
    public partial class HipoteseDiagnostica : UserControl
    {
        #region Value

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
            typeof(ct_hipoteseDiagnostica), typeof(HipoteseDiagnostica), new PropertyMetadata(null));

        public ct_hipoteseDiagnostica Value
        {
            get { return (ct_hipoteseDiagnostica)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        #endregion

        public HipoteseDiagnostica()
        {
            this.DataContext = this;

            if (this.Value == null)
            {
                this.Value = new ct_hipoteseDiagnostica()
                {
                    CIDPrincipal = new ct_diagnosticoGuia()
                    {
                        CID = new ct_diagnostico()
                        {
                            codigoDiagnostico = string.Empty,
                            descricaoDiagnostico = string.Empty,
                            nomeTabela = st_tabelasDiagnostico.CID10
                        },
                        indicadorAcidente = st_indicadorAcidente.Item0,
                        indicadorAcidenteSpecified = false,
                        tempoReferidoEvolucaoDoenca = new ct_tempoEvolucaoDoenca()
                        {
                            unidadeTempo = st_unidadeTempo.A,
                            valor = string.Empty
                        },
                        tipoDoenca = st_tipoDoenca.A,
                        tipoDoencaSpecified = false
                    },
                    diagnosticosSecundarios = new List<ct_diagnostico>(new ct_diagnostico[]
                        {
                            new ct_diagnostico()
                            {
                                codigoDiagnostico = string.Empty,
                                descricaoDiagnostico = string.Empty,
                                nomeTabela = st_tabelasDiagnostico.CID10
                            }
                        })
                };
            }

            InitializeComponent();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (this.Value.diagnosticosSecundarios.Count < 3)
            {
                var list = this.Value.diagnosticosSecundarios.GetRange(0, this.Value.diagnosticosSecundarios.Count);

                list.Add(new ct_diagnostico()
                {
                    codigoDiagnostico = string.Empty,
                    descricaoDiagnostico = string.Empty,
                    nomeTabela = st_tabelasDiagnostico.CID10,
                });

                this.Value.diagnosticosSecundarios = list;
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            ContentControl ctrl = sender as ContentControl;

            var diagnosticoRemovido = ctrl.DataContext as ct_diagnostico;

            if (this.Value.diagnosticosSecundarios.Count > 1)
            {
                var list = this.Value.diagnosticosSecundarios.GetRange(0, this.Value.diagnosticosSecundarios.Count);

                list.Remove(diagnosticoRemovido);

                this.Value.diagnosticosSecundarios = list;
            }
        }

        private void cbxDiag_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cbx = sender as CheckBox;
            FrameworkElement element = cbx.Parent as FrameworkElement;

            Button btn = element.FindName("btnAdd") as Button;
            ItemsControl itemControl = element.FindName("diagnosticoSecundarioControl") as ItemsControl;

            btn.Visibility = cbx.IsChecked.HasValue && cbx.IsChecked.Value ?
                System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;

            itemControl.Visibility = cbx.IsChecked.HasValue && cbx.IsChecked.Value ?
                System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }

        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var CIDUserControl = (sender as FrameworkElement).FindName("CIDUserControl") as CID;

            var current = (sender as FrameworkElement).DataContext as ct_diagnostico;

            Binding b = new Binding();

            b.Path = new PropertyPath("diagnostico");
            b.Mode = BindingMode.TwoWay;
            b.ValidatesOnDataErrors = true;
            b.NotifyOnValidationError = true;
            b.Source = current;

            CIDUserControl.SetBinding(CID.ValueProperty, b);
        }
    }
}
